<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

    <title> - 登录</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/login.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->
    <script>
        if (window.top !== window.self) {
            window.top.location = window.location;
        }
    </script>

</head>

<body class="signin">
    <div class="signinpanel">
        <div class="row">
            <div class="col-sm-12">
                <form id="login_form" method="post" onsubmit="return false" style = "width:400px">
                    <h4 class="no-margins">登录：</h4>
                    <p class="m-t-md">欢迎您</p>
                    <input type="text" id="name" name="name" class="form-control uname" placeholder="用户名" />
                    
                   
                    <input type="password" id="password" name="password" class="form-control pword m-b" placeholder="密码" />
                 	<input type="text" name="checkCode" class="form-control pword m-b" placeholder="验证码">
					<img id="codeImgId" src="/JAVA_WEB_1907/checkImg" onclick="refreshCode()"><br/>
                    <a href="">忘记密码了？</a>
                    <button class="btn btn-success btn-block" onclick="submitForm()">登录</button>
                </form>
            </div>
        </div>
        <div class="signup-footer">
            <div class="pull-left">
                
            </div>
        </div>
    </div>
    
    <script src="js/jquery-1.11.3.js" type="text/javascript" charset="utf-8"></script>
    <script src="lib/layer/layer.js" type="text/javascript" charset="utf-8"></script>
    <script src="lib/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
    	function submitForm() {
    		console.log("submitForm");
			// 1、	验证用户名
			// 1.1	用户名不能为空
			// 1.2	格式是否合法：4-8位数字或字母
			var name = $("#name").val();
			var password = $("#password").val();
			// 1.1	用户名不能为空
			if (name == "" || name == undefined || name == null) {
				mylayer.errorMsg("用户名不能为空");
				return;
			}
			// 1.2	格式是否合法：4-8位数字或字母
			var pattern = /^[0-9a-zA-Z]{4,8}$/;
			// if (!pattern.test(name)) {
			if (pattern.test(name) == false) {
				mylayer.errorMsg("用户名格式非法，4-8位数字或字母");
				return;
			}
			// 2、密码验证
			// 2.1	用户名不能为空
			// 2.2	格式是否合法
			
			// 2.1	用户名不能为空
			if (password == "" || password == undefined || password == null) {
				mylayer.errorMsg("密码不能为空");
				return;
			}
			// 2.2	格式是否合法
			var pattern = /^[0-9a-zA-Z]{3}$/;
			// if (!pattern.test(password)) {
			if (pattern.test(password) == false) {
				mylayer.errorMsg("密码格式非法，3位数字或字母");
				return;
			}
			
			// 3、ajax提交用户名和密码到后台，验证是不是存在，返回ServerResponse对象，
			// 前台根据返回的ServerResponse决定响应的操作
			// 3.1 code=SUCCESS 登录成功，mylayer.successUrl("/student?method=pageList");
			// 3.2 code=ERROR 弹出msg里面的提示失败信息 mylayer.error(serverResponse.msg);
			console.log($("#login_form").serialize());
			$.ajax({
				url : "${pageContext.request.contextPath}/loginServlet?method=login",
				type : "post",
				dataType : "json",
				data : $("#login_form").serialize(),
				success : function(jsonObj) {
					// {code:1,msg:"登录成功"}					
					if(jsonObj.code == 1) {
						mylayer.successMsgUrl(jsonObj.msg, "${pageContext.request.contextPath}/student?method=pageList");
					} else {
						// {"code":0,"count":0,"data":null,"msg":"验证码错误"}
						mylayer.errorMsg("用户名或密码错误");
					}
				}
			});
    	}
    	function refreshCode() {
			$("#codeImgId").attr("src", "<%=request.getContextPath()%>/checkImg?" + Math.random());
		}
	</script>
    
</body>

</html>
